<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
	Object obj = request.getSession().getAttribute("role_resource");
	if (null == obj) {
		response.setStatus(1000);
		return;
	}
%>
<html>
	<style type="text/css">
.ext-el-mask {
	background-color: #FFFFFF;
}
</style>
	<script type="text/javascript">
var dbclicktag=true;
function show_img(path) {
	var win = Ext.getCmp("showimages");
	win.show();
	var html = "<img id='imgpage' width=" + win.getInnerWidth() + " height="
			+ win.getInnerHeight() + "  src='download.action?id="
			+ encodeURI(path) + "' />";
	win.update(html);
};

function setRulePanelDisable(flag) {
	Ext.getCmp("idxField").setDisabled(flag);
	Ext.getCmp("opr1Field").setDisabled(flag);
	Ext.getCmp("opr2Field").setDisabled(flag);
	Ext.getCmp("cifField").setDisabled(flag);
	Ext.getCmp("typeField").setDisabled(flag);
	Ext.getCmp("fodderField").setDisabled(flag);
	Ext.getCmp("priField").setDisabled(flag);
}

var img_index = 0;//图片路径下表
var prod_opera;//传给ajax url属性的action字符串
var opt = false;//增加规则按钮操作标示
var ISVIEW = false;
var check_save1 = 0; //从表修改标识，0保存，1未保存
var check_save2 = 0; //从从表修改标识，0保存，1未保存

//状态下拉列表
var state_store = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "0",
		text : "退回"
	}, {
		id : "1",
		text : "启用"
	}, {
		id : "2",
		text : "草稿"
	}, {
		id : "3",
		text : "审核中"
	} ]
});
//规则类型下拉列表
var type_store = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "1",
		text : "自定义"
	}, {
		id : "0",
		text : "默认"
	} ]
});

//模板下拉列表
var temp_sotre = new Ext.data.JsonStore( {
	root : "data",
	fields : [ "id", "name", "type" ],
	url : "template!listAll.action"
});

//搜索条件规则类型下拉列表
var type_store1 = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "",
		text : "全部"
	}, {
		id : "1",
		text : "自定义"
	}, {
		id : "0",
		text : "默认"
	} ]
});
//搜索条件模板类型下拉列表
var type_store2 = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "",
		text : "全部"
	}, {
		id : "1",
		text : "纸质"
	}, {
		id : "2",
		text : "电子"
	} ]
});

//个性化区域
var area_store = new Ext.data.JsonStore( {
	root : "data",
	totalProperty : "total",
	fields : [ "id", "name", "type", "remark" ],
	url : "templatearea!list.action"
});

//个性化区域规则数据源
var store = new Ext.data.JsonStore( {
	root : "data",
	totalProperty : "total",
	fields : [ "rule_no", "rule_name", "rule_type", "stencil_no",
			"stencil_name", "stencil_ver", "stenciltype_no", "period_begen",
			"period_end", "rule_pri", "person_no", "create_time", "remark",
			"areaNo", "areaName", "rule_state", "fodderType" ],
	url : "rule!etGridList.action"
});

//个性化区域规则从表数据源
var f_store = new Ext.data.JsonStore( {
	root : "data",
	totalProperty : "total",
	fields : [ "id", "ruleno", "fodder","pri","fodderName","fodderType"],
	url : "rule!listRuleParam.action"
});

//素材数据结果集
var fdata = new Ext.data.JsonStore( {
	root : "data",
	totalProperty : "total",
	baseParams : {
		"fodder.state" : 1
	},
	fields : [ "id", "name", "type", "docname", "begin", "end", "state", "url",
			"remark", "personno", "personname", "createtime" ],
	url : "fodder!list.action"
});
//素材状态下拉列表
var fodder_state_store = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "0",
		text : "退回"
	}, {
		id : "1",
		text : "启用"
	}, {
		id : "2",
		text : "草稿"
	}, {
		id : "3",
		text : "审核中"
	} ]
});

var rule_record = Ext.data.Record.create( [ "id", "ruleno", "fodder", "pri" ]);
//规则模拟数据
var rule_store = new Ext.data.JsonStore(
		{
			root : "data",
			idProperty : "idx",
			fields : [ "id", "idx", "opr1", "value1", "opr2", "value2", "cif",
					"type" ,"valueName1","valueName2"],
			url : "rule!listRuleOper.action"
		});
var ope_store = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "0",
		text : "等于"
	}, {
		id : "1",
		text : "大于"
	}, {
		id : "2",
		text : "小于"
	}, {
		id : "3",
		text : "大于等于"
	}, {
		id : "4",
		text : "小于等于"
	} ]
});

var lj_store = new Ext.data.JsonStore( {
	fields : [ "id", "text" ],
	data : [ {
		id : "0",
		text : "或者"
	}, {
		id : "1",
		text : "并且"
	} ]
});

var ljRenderer = function(input) {
	if ("" == input)
		return input;
	var res1 = lj_store.getById(input);
	return res1.data.text;
};

var field_store = new Ext.data.JsonStore( {
	root : "data",
	fields : [ "id", "text" ],
	url : "billFodder!listField.action"
});
var oprRenderer = function(input) {
	if (!input || "" == input)
		return input;
	var res1 = ope_store.getById(input);
	return res1.data.text;
};
var fieldRenderer = function(input) {
	if ("" == input)
		return input;
	var res3 = field_store.getById(input);
	return res3.data.text;
};
var opecombox = {
	xtype : "billCombox",
	store : ope_store
};
var fieldcombox = {
	xtype : "billCombox",
	id : "typeField",
	store : field_store,
	listeners:{
		'select' : function (  combo ,  record ,  index  ) {
			//选择后清空value1和value2的值
			personrule2.getStore().getAt(ruleRow).data.value1='';
			personrule2.getStore().getAt(ruleRow).data.value2='';
			personrule2.getStore().getAt(ruleRow).data.valueName1='';
			personrule2.getStore().getAt(ruleRow).data.valueName2='';
			personrule2.getStore().getAt(ruleRow).data.opr1='0';
			personrule2.getStore().getAt(ruleRow).data.opr2='';
		}
	}
};
//逻辑符下拉列表
var ljcombox = {
	id : 'cifField',
	xtype : "billCombox",
	valueField : "id",
	displayField : "text",
	store : lj_store
};
//图片路径,上传
var image_store = new Ext.data.JsonStore( {
	root : "data",
	fields : [ "path" ],
	url : "template!queryImage.action"
});

var image_record = Ext.data.Record.create( [ "path" ]);

//修改时用的变量
var stenciltype;

var sel = function() {
	var node = Ext.getCmp("prod_tree").getSelectionModel().getSelectedNode();
	if (node && node.isLeaf()) {

		store.baseParams = store.baseParams || {};

		store.baseParams["ruleMaster.rule_type"] = Ext.getCmp("a").getValue();
		store.baseParams["ruleMaster.remark"] = Ext.getCmp("b").getValue();
		store.baseParams["ruleMaster.stenciltype_no"] = Ext.getCmp("c")
				.getValue();

		store.load( {
			params : {
				start : 0,
				limit : limit
			}
		});
	} else {
		Ext.Msg.show( {
			title : "警告",
			msg : "未选中任何行卡产品信息,无法执行此操作!",
			buttons : Ext.Msg.OK,
			animEl : "elId",
			icon : Ext.MessageBox.WARNING
		});
	}
};

var p1 = {
	id : "p1Grid",
	xtype : "grid",
	loadMask : true,
	autoExpandColumn : "rule_name",//自动填充
	autoExpandMin : 100,//自动填充最小宽度
	store : store,
	headersDisabled : true,
	sm : new Ext.grid.RowSelectionModel( {//行选择监听,判断状态是否可审核
				listeners : {
					"rowselect" : function(s, index) {
						var state = Ext.getCmp("p1Grid").getSelectionModel()
								.getSelected().data.rule_state;
						if (state == "1" || state == "3") {//启用不能修改,不能审核
							Ext.getCmp("del_temp").setDisabled(true);
							Ext.getCmp("edit_temp").setDisabled(true);
						} else {
							Ext.getCmp("del_temp").setDisabled(false);
							Ext.getCmp("edit_temp").setDisabled(false);
						}

						if (state == "0") {
							Ext.getCmp("del_temp").setDisabled(true);
						}
					}
				}
			}),
	columns : [
			new Ext.grid.RowNumberer( {
				header : "序号",
				width : 35
			}),
			{
				dataIndex : "rule_no",
				header : "规则编号",
				menuDisabled : true
			},
			{
				dataIndex : "rule_name",
				header : "规则名称",
				id : "rule_name",
				menuDisabled : true
			},
			{
				dataIndex : "rule_type",
				header : "规则类别",
				menuDisabled : true,
				renderer : function(input) {
					if ("0" == input)
						return "默认";
					else if ("1" == input)
						return "自定义";
				}
			},
			{
				dataIndex : "stencil_name",
				header : "模版名称",
				menuDisabled : true
			},
			{
				dataIndex : "stencil_ver",
				header : "模板版本号",
				menuDisabled : true
			},
			{
				dataIndex : "stenciltype_no",
				header : "模版类型编号",
				menuDisabled : true,
				renderer : function(input) {
					if ("1" == input)
						return "纸质";
					else if ("2" == input)
						return "电子";
					else if ("3" == input)
						return "短信";
				}
			},
			{
				dataIndex : "period_begen",
				header : "账期开始月",
				menuDisabled : true
			},
			{
				dataIndex : "period_end",
				header : "账期截止月",
				menuDisabled : true
			},
			{
				dataIndex : "rule_pri",
				header : "优先级",
				menuDisabled : true
			},
			{
				dataIndex : "rule_state",
				header : "状态",
				menuDisabled : true,
				renderer : function(input) {
					if ("0" == input)
						return "退回";
					else if ("1" == input)
						return "启用";
					else if ("2" == input)
						return "草稿";
					else if ("3" == input)
						return "审核中";
				}
			},
			{
				dataIndex : "remark",
				header : "备注",
				menuDisabled : true
			},
			{
				xtype : "actioncolumn",
				header : "详细",
				width : 34,
				id : "detail",
				items : [ {
					icon : "images/shared/icons/fam/information.png", // Use a URL in the icon config
					tooltip : "详细",
					handler : function(grid, rowIndex, colIndex) {
						var record = store.getAt(rowIndex);
						stenciltype = record.data.stenciltype_no;
						rule_store.removeAll();
						area_store.load( {
									params : {
										'temparea.tempno' : record.data.stencil_no
									},
									callback : function() {
										Ext.getCmp("rule_panel").layout
												.setActiveItem(1);
										Ext.getCmp("areaform").setParam(
												record.data);
										Ext.getCmp("areaform")
												.setDisabled(true);//disabled=true;
										Ext.getCmp("personrule")
												.getTopToolbar().setDisabled(
														true);
										Ext.getCmp("personrule2")
												.getTopToolbar().setDisabled(
														true);
										Ext.getCmp("submitStart").setVisible(
												false);
										Ext.getCmp("cancelButton")
												.setText('返回');
										Ext.getCmp("saveButton").setVisible(
												false);
										Ext.getCmp("personrule").isEditor = false;
										ISVIEW = true;
										setRulePanelDisable(true);

										//加载区域规则
										image_store
												.load( {
													params : {
														"template.id" : record.data.stencil_no
													},
													callback : function() {
														var win = Ext.getCmp("imagePanel");
														win.show();
														img_index = 0;
														var page_num = "1/"+ image_store.getCount();
														Ext.getCmp("show_num").setText(page_num);
														var html = "<a href=javascript:show_img('"+ encodeURI(this.getAt(0).get('path'))+ "');>"
																+ "<img width="+ win.getInnerWidth()+ " height="+ win.getInnerHeight()
																+ " src='download.action?id="+ encodeURI(this.getAt(0).get('path'))+ "'/></a>";
														win.update(html);
													}
												});
										f_store.load( {
													params : {
														"ruleMaster.rule_no" : record.data.rule_no,
														"ruleMaster.areaNo" : record.data.areaNo
													}
												});
									}
								});
					}
				} ]
			} ],
	bbar : new Ext.ux.PagingToolbar( {
		pageSize : 18,
		store : store
	}),
	buttonAlign : "center",
	buttons : [
			{
				text : "新增",
				handler : function() {
					Ext.getCmp("rulePanel_add_but").setDisabled(true); //设置增加规则按钮为不可用
					
					if(Ext.getCmp("imagePanel")){
					Ext.getCmp("imagePanel").html = "" ;
					}
					
					if(Ext.getCmp("show_num")){
					Ext.getCmp("show_num").setText("0/0");
					}
					
					prod_opera = "rule!add.action";
					var panel = this.ownerCt.ownerCt.ownerCt;
					var node = Ext.getCmp("prod_tree").getSelectionModel().getSelectedNode();//当前选中的树节点
					if (node && node.isLeaf()) {
						Ext.Ajax.request( {
									url : "rule!etRuleNo.action",
									success : function(response, opts) {
										var d = eval("("+ response.responseText + ")");
										if (d.code == "1") {
											Ext.getCmp("areaform").resetParam(
													node.id, d.data);
											panel.layout.setActiveItem(1);

											Ext.getCmp("ruleType").setDisabled(
													false);
											Ext.getCmp("stencilNo")
													.setDisabled(false);
											Ext.getCmp("area_no").setDisabled(
													false);
											Ext.getCmp('rule_name')
													.setDisabled(false);
											Ext.getCmp("ruleState").setValue(
													'2');
											Ext.getCmp("submitStart")
													.setVisible(false);
											Ext.getCmp("fodder_type").setValue(
													"");
											if(area_store){
												area_store.removeAll();
											}
											rule_store.removeAll();
											image_store.removeAll();
										} else {
											Ext.Msg.show( {
												title : "提示",
												msg : "操作失败!",
												buttons : Ext.Msg.OK,
												icon : Ext.MessageBox.ERROR
											});
										}
										f_store.load( {
											params : {
												"ruleMaster.rule_no" : d.data,
												"ruleMaster.areaNo" : 1
											}
										});
									}
								});
					} else {
						Ext.Msg.show( {
							title : "警告",
							msg : "没有选中任何行卡产品，无法进行此操作!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});
					}
				}
			},
			{
				text : "修改",
				id : 'edit_temp',
				handler : function() {
					Ext.getCmp("rulePanel_add_but").setDisabled(false); //允许增加规则
				prod_opera = "rule!upd.action";
				var record = Ext.getCmp("p1Grid").getSelectionModel().getSelected();
				if (record) {
					if (record.data.rule_state == 1) {
						Ext.Msg.show( {
							title : "警告",
							msg : "该区域规则已启用，现不能做修改!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});
						return;
					}
					if (record.data.rule_state == 3) {
						Ext.Msg.show( {
							title : "警告",
							msg : "该区域规则在审核中，现不能做修改!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});
						return;
					}
					stenciltype = record.data.stenciltype_no;
					rule_store.removeAll();
					area_store.load( {
						params : {
							"temparea.tempno" : record.data.stencil_no
						},
						callback : function() {
							Ext.getCmp("rule_panel").layout.setActiveItem(1);

							Ext.getCmp("areaform").setParam(record.data);

							Ext.getCmp('ruleType').setDisabled(true);
							Ext.getCmp('stencilNo').setDisabled(true);
							Ext.getCmp('area_no').setDisabled(true);
							Ext.getCmp('rule_name').setDisabled(true);
							Ext.getCmp("submitStart").setVisible(true);
							Ext.getCmp("saveButton").setDisabled(false);
							
							//加载区域规则
							image_store.load( {
								params : {
									"template.id" : record.data.stencil_no
								},
								callback : function() {
									var win = Ext.getCmp("imagePanel");
									win.show();
									img_index = 0;
									var page_num = "1/"+ image_store.getCount();
									Ext.getCmp("show_num").setText(page_num);
									
									var html = "<a href=javascript:show_img('"
											+ encodeURI(this.getAt(0).get('path'))
											+ "');>"
											+ "<img width="
											+ win.getInnerWidth()
											+ " height="
											+ win.getInnerHeight()
											+ " src='download.action?id="
											+ encodeURI(this.getAt(0).get(
													'path')) + "'/></a>";
									win.update(html);
								}
							});
							
								f_store.load( {
									params : {
										"ruleMaster.rule_no" : record.data.rule_no,
										"ruleMaster.areaNo" : record.data.areaNo
									}
								});
						}
					});
					
				} else {
					Ext.Msg.show( {
						title : "警告",
						msg : "没有选中任何行卡产品，无法进行修改操作!",
						buttons : Ext.Msg.OK,
						animEl : "elId",
						icon : Ext.MessageBox.WARNING
					});
				}
			}
			},
			{
				text : "删除",
				id : 'del_temp',
				handler : function() {
					prod_opera = "rule!del.action";
					var p1 = this.ownerCt.ownerCt;
					var record = Ext.getCmp("p1Grid").getSelectionModel()
							.getSelected();
					if (record) {
						if (record.data.rule_state == 1) {
							Ext.Msg.show( {
								title : "警告",
								msg : "该区域规则已启用，现不能做删除!",
								buttons : Ext.Msg.OK,
								animEl : "elId",
								icon : Ext.MessageBox.WARNING
							});
							return;
						}
						if (record.data.rule_state == 3) {
							//Ext.Msg.alert('错误','该区域规则在审核中，现不能做删除！');
							Ext.Msg.show( {
								title : "警告",
								msg : "该区域规则在审核中，现不能做删除!",
								buttons : Ext.Msg.OK,
								animEl : "elId",
								icon : Ext.MessageBox.WARNING
							});
							return;
						}
						Ext.Msg.confirm(
										"警告",
										"确定要删除吗?",
										function(btn) {
											if (btn == "yes") {
												Ext.Ajax
														.request( {
															url : prod_opera,
															params : {
																"ruleMaster.rule_no" : record.data.rule_no
															},
															success : function(
																	response,
																	opts) {
																var d = eval('(' + response.responseText + ')');
																if (d.code == '1') {
																	Ext
																			.getCmp(
																					'p1Grid')
																			.getBottomToolbar()
																			.doRefresh();
																	Ext.Msg
																			.show( {
																				title : "提示",
																				msg : "操作成功!",
																				buttons : Ext.Msg.OK,
																				icon : Ext.MessageBox.INFO
																			});

																} else {
																	Ext.Msg
																			.show( {
																				title : "提示",
																				msg : "操作失败!",
																				buttons : Ext.Msg.OK,
																				icon : Ext.MessageBox.ERROR
																			});
																}
															}
														});
											}
										});
					} else {
						Ext.Msg.show( {
							title : "警告",
							msg : "没有选中任何行卡产品，无法进行删除操作!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});
					}

				}
			} ],
	tbar : [ 
		"&nbsp&nbsp规则类别:", 
		{
		id : "a",
		xtype : "billCombox",
		value:'',
		width : 80,
		store : type_store1,
		listeners : {
			"select" : sel
		}
	}, "&nbsp&nbsp模板类型:" , 
		{
		id : "c",
		xtype : "billCombox",
		value:'',
		width : 80,
		store : type_store2,
		listeners : {
			"select" : sel
		}
	} , "&nbsp&nbsp模板名称:", 
		{
		id : "b",
		enableKeyEvents:true,
		xtype : "textfield",
		listeners : {
			'keypress' : function (  tobj ,   e  ){
				if(e.getKey()==e.ENTER)sel();
			} 
		}
	}]
};

var org_add_update = 0;

var p2 = {
	id : "areaform",
	columnWidth : 1,
	height : 302,
	xtype : "form",
	title : "个性化区域信息",
	bodyStyle : "padding:5px 5px 0",
	defaultType : "textfield",
	items : [
			{
				itemId : "cardno",
				hidden : true,
				name : "cardno"
			},
			{
				itemId : "rule_no",
				id : "ruleno",
				fieldLabel : "规则编号",
				hidden : true,
				anchor : "100%",
				name : "rule_no"
			},
			{
				itemId : "rule_name",
				id : "rule_name",
				fieldLabel : "规则名称",
				anchor : "100%",
				allowBlank : false,
				vtype : "alphanumchinese",
				validator : function(val) {
					if (val == "")
						return "该输入项为必输项!";
					if (Ext.fucCheckCHLength(val) > 50) {
						return "请输入50个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
					}
					return true;
				},
				name : "rule_name"
			},
			{
				itemId : "rule_type",
				id : "ruleType",
				fieldLabel : "类别",
				allowBlank : false,
				name : "rule_type",
				xtype : "billCombox",
				store : type_store
			},
			{
				itemId : "stencil_no",
				id : "stencilNo",
				fieldLabel : "模板名称",
				name : "stencil_no",
				allowBlank : false,
				xtype : "billCombox",
				store : temp_sotre,
				valueField : "id",
				displayField : "name",
				listeners : {
					"select" : function() {
						area_store.load( {
							params : {
								"temparea.tempno" : this.value
							}
						});
						image_store.load( {
							params : {
								"template.id" : this.value
							},
							callback : function() {
								var win = Ext.getCmp("imagePanel");
								win.show();
								img_index = 0;
								var page_num = "1/"+ image_store.getCount();
								Ext.getCmp("show_num").setText(page_num);
								var html = "<a href=javascript:show_img('"
										+ encodeURI(this.getAt(0).get('path')) + "');>"
										+ "<img width=" + win.getInnerWidth()
										+ " height=" + win.getInnerHeight()
										+ " src='download.action?id="
										+ encodeURI(this.getAt(0).get('path'))
										+ "'/></a>";
								win.update(html);
							}
						});
						this.ownerCt.getComponent("areaNo").setValue("");
					}
				}
			},
			{
				itemId : "areaNo",
				fieldLabel : "区域名称",
				id : "area_no",
				name : "areaNo",
				allowBlank : false,
				xtype : "billCombox",
				store : area_store,
				valueField : "id",
				displayField : "name",
				listeners : {
					"select" : function(combo, record, index) {
						var type = area_store.getById(record.id).data.type;
						this.ownerCt.getComponent("fodderType").setValue(type);
					}
				}
			},
			{
				itemId : "fodderType",
				fieldLabel : "区域素材类型",
				id : "fodder_type",
				name : "fodderType",
				xtype : "billCombox",
				disabled : true,
				store : new Ext.data.JsonStore( {
					fields : [ "id", "name" ],
					data : [ {
						id : "1",
						name : "图片"
					}, {
						id : "2",
						name : "文本"
					}, {
						id : "3",
						name : "列表"
					}, {
						id : "4",
						name : "自动填充"
					} ]
				}),
				valueField : "id",
				displayField : "name"
			},
			{
				itemId : "period_begen",
				fieldLabel : "账期开始月",
				anchor : "100%",
				vtype : "daterange",
				endDateField : "periodEnd",
				allowBlank : false,
				name : "period_begen",
				id : "periodBegen",
				xtype : "datefield",
				format : "Y-m",
				editable : false
			},
			{
				itemId : "period_end",
				fieldLabel : "账期截止月",
				anchor : "100%",
				id : "periodEnd",
				allowBlank : false,
				name : "period_end",
				vtype : "daterange",
				startDateField : "periodBegen",
				xtype : "datefield",
				format : "Y-m",
				editable : false
			},
			{
				itemId : "rule_pri",
				fieldLabel : "优先级",
				anchor : "100%",
				name : "rule_pri",
				xtype : "numberfield",
				id : "rulePri",
				validator : function(val) {
					if (!(/^[\d]{1,2}$/).test(val) || parseInt(val) < 1
							|| parseInt(val) > 99) {
						return "必须设置为1到99之间的整数！";
					}
				}
			}, {
				itemId : "rule_state",
				fieldLabel : "状态",
				allowBlank : false,
				name : "rule_state",
				id : "ruleState",
				disabled : true,
				xtype : "billCombox",
				store : state_store

			}, {
				itemId : "remark",
				fieldLabel : "备注",
				anchor : "100%",
				vtype : "alphanumchinese",
				validator : function(val) {
					if (val == "")
						return true;
					if (Ext.fucCheckCHLength(val) > 200) {
						return "请输入200个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
					}
					return true;
				},
				name : "remark"
			} ],
	buttonAlign : "center",
	setParam : function(data) {
		this.getComponent("cardno").setValue(data.card_no);
		this.getComponent("rule_no").setValue(data.rule_no);
		this.getComponent("rule_no").setDisabled(true);
		this.getComponent("rule_name").setValue(data.rule_name);
		this.getComponent("rule_type").setValue(data.rule_type);
		this.getComponent("stencil_no").setValue(data.stencil_no);
		this.getComponent("areaNo").setValue(data.areaNo);
		this.getComponent("period_begen").setValue(data.period_begen);
		this.getComponent("period_end").setValue(data.period_end);
		this.getComponent("rule_pri").setValue(data.rule_pri);
		this.getComponent("rule_state").setValue(data.rule_state);
		this.getComponent("areaNo").setValue(data.areaNo);
		this.getComponent("remark").setValue(data.remark);
		this.getComponent("fodderType").setValue(data.fodderType);
	},
	resetParam : function(cardno, ruleno) {
		this.getComponent("rule_no").setDisabled(true);
		this.getComponent("rule_no").setValue(ruleno);
		this.getComponent("cardno").setValue(cardno);
		this.getComponent("rule_name").setValue("");
		this.getComponent("rule_type").setValue("");
		this.getComponent("stencil_no").setValue("");
		this.getComponent("period_begen").setValue("");
		this.getComponent("period_end").setValue("");
		this.getComponent("rule_pri").setValue("");
		this.getComponent("rule_state").setValue("");
		this.getComponent("areaNo").setValue("");
		this.getComponent("remark").setValue("");
	}
};
var smpaper = new Ext.grid.CheckboxSelectionModel( {
	singleSelect : true
});// 添加复选框
//预印纸数据源
var paper_store = new Ext.data.JsonStore( {
	root : "data",
	totalProperty : "total",
	fields : [ "pid", "paper_no", "paper_name", "paper_weight", "person_no",
			"create_time", "period_begin", "period_end", "period_day",
			"businpnt_name", "businpnt_no" ],
	url : "paper!paperByTimeCard.action"
});

var paper = {
	id : "paper",
	xtype : "grid",
	loadMask : true,
	store : paper_store,
	sm : smpaper,
	headersDisabled : true,
	columns : [ new Ext.grid.RowNumberer( {
		header : "序号",
		width : 35
	}), smpaper, {
		dataIndex : "pid",
		header : "序列号",
		menuDisabled : true
	}, {
		dataIndex : "paper_no",
		header : "预印纸编号",
		menuDisabled : true
	}, {
		dataIndex : "businpnt_name",
		header : "外包商",
		menuDisabled : true
	}, {
		dataIndex : "paper_name",
		header : "名称",
		menuDisabled : true
	}, {
		dataIndex : "paper_weight",
		header : "克重",
		menuDisabled : true
	}, {
		dataIndex : "period_begin",
		header : "有效开始月",
		menuDisabled : true
	}, {
		dataIndex : "period_end",
		header : "有效截止月",
		menuDisabled : true
	}, {
		dataIndex : "period_day",
		header : "账单日",
		menuDisabled : true
	}, {
		dataIndex : "person_no",
		header : "创建人",
		menuDisabled : true
	}, {
		dataIndex : "create_time",
		header : "创建时间",
		menuDisabled : true
	} ],
	bbar : new Ext.ux.PagingToolbar( {
		pageSize : limit,
		store : paper_store
	})
};

var paperwin=null;
////////////////////////////////////////////////////////////////
var win;

var sm = new Ext.grid.CheckboxSelectionModel( {
	singleSelect : true
});// 添加复选框

//选择素材
var selectFodder = {
	id : "selectFodder",
	xtype : "grid",
	stripeRows : true,
	autoExpandColumn : "name",
	autoExpandMin : 100,
	store : fdata,
	sm : sm,
	headersDisabled : true,
	columns : [
			new Ext.grid.RowNumberer(),
			sm,
			{
				header : "编号",
				dataIndex : "id",
				menuDisabled : true
			},
			{
				header : "素材名称",
				dataIndex : "name",
				id : "name",
				menuDisabled : true
			},
			{
				header : "素材类型",
				dataIndex : "type",
				menuDisabled : true,
				renderer : function(data) {
					if ("1" == data)
						return "图片";
					else if ("2" == data)
						return "文本";
					else if ("3" == data)
						return "列表";
				}
			},
			{
				header : "文件名",
				dataIndex : "docname",
				menuDisabled : true
			},
			{
				header : "有效期起始月",
				dataIndex : "begin",
				menuDisabled : true
			},
			{
				header : "有效期截止月",
				dataIndex : "end",
				menuDisabled : true
			},
			{
				header : "状态",
				dataIndex : "state",
				menuDisabled : true,
				renderer : function(input) {
					var res4 = fodder_state_store.getById(input);
					if (res4) {
						return (res4) ? res4.data.text : "";
					}
					return "";
				}
			},
			{
				header : "URL",
				dataIndex : "url",
				menuDisabled : true
			},
			{
				header : "备注",
				dataIndex : "remark",
				menuDisabled : true
			},
			{
				header : "操作人",
				dataIndex : "personname",
				menuDisabled : true
			},
			{
				header : "创建时间",
				dataIndex : "createtime",
				menuDisabled : true
			},
			{
				xtype : "billActionColumn",
				header : "预览",
				width : 34,
				dataIndex : "type",
				items : [ {
					icon : "images/shared/icons/fam/information.png", // Use a URL in the icon config
					tooltip : "预览",
					handler : function(grid, rowIndex, colIndex) {
						var rec = fdata.getAt(rowIndex);
						var win = Ext.getCmp("showimages");
						win.show();
						var html = "<img id='imgpage' width="
								+ win.getInnerWidth() + " height="
								+ win.getInnerHeight()
								+ " src='download.action?id="
								+ encodeURI(rec.get('docname')) + "' />";
						win.update(html);
					}
				} ],
				renderer : function(input) {
					return "1" == input;
				}
			} ],
	bbar : new Ext.ux.PagingToolbar( {
		pageSize : limit,
		store : fdata
	})
};

var cm = new Ext.grid.ColumnModel(
		{
			defaults : {
				sortable : true
			},
			columns : [
					{
						id : "id",
						header : "序号",
						dataIndex : "id",
						width : 35,
						menuDisabled : true
					},
					{
						id : "fodder",
						header : "素材名称",
						dataIndex : "fodderName",
						width : 114,
						menuDisabled : true,
						editor : new Ext.form.TextField( {
							id : "fodderField",
							allowBlank : false
						})

					},
					{
						id : "pri",
						header : "优先级",
						dataIndex : "pri",
						width : 80,
						menuDisabled : true,
						editor : new Ext.form.NumberField( {
							allowBlank : false,
							id : "priField",
							validator : function(val) {
								if (val == "")
									return false;

								if (val >= 1)
									return true;
								else
									return false;
							}
						})
					},
					{
						xtype : "actioncolumn",
						header : "预览",
						width : 34,
						menuDisabled : true,
						items : [ {
							icon : "images/shared/icons/fam/information.png", // Use a URL in the icon config
							tooltip : "预览",
							handler : function(grid, rowIndex, colIndex) {
								var fodd=f_store.getAt(rowIndex).data;
								if(fodd){
									if (""==fodd.fodder) {
										alertMsg("请先选择素材!");
										return;
									}
								}else{
									return;
								}
								var modifiedArray = personrule.getStore().getModifiedRecords();
								
								if (modifiedArray.length > 0){
									alertMsg("请先保存数据!");
									return;
								}
									
								if (stenciltype == "1" || stenciltype == 1) {
									paper_store.load( {
												params : {
													"paper.period_begin" : Ext
															.getCmp("areaform")
															.getComponent("period_begen")
															.getValue().format("Y-m"),
													"paper.period_end" : Ext
															.getCmp("areaform")
															.getComponent("period_end")
															.getValue().format("Y-m"),
													"paper.pid" : Ext
															.getCmp("prod_tree")
															.getSelectionModel()
															.getSelectedNode().id
												},
												callback : function() {
													if (paper_store.data.items.length < 1) {
														Ext.Msg.show( {
																	title : "警告",
																	msg : "此卡产品在有效期内没有预印纸!",
																	buttons : Ext.Msg.OK,
																	animEl : "elId",
																	icon : Ext.MessageBox.WARNING
																});

													} else {
														if (!paperwin) {

															paperwin = new Ext.Window(
																	{
																		layout : "fit",
																		width : 1100,
																		height : 500,
																		closeAction : "hide",
																		plain : true,
																		items : [ paper ],
																		buttons : [
																				{
																					text : "确认",
																					handler : function() {
																						if (!Ext.getCmp("paper").getSelectionModel().getSelected()) {
																							Ext.Msg.show( {
																										title : "警告",
																										msg : "请选择预印纸!",
																										buttons : Ext.Msg.OK,
																										animEl : "elId",
																										icon : Ext.MessageBox.WARNING
																									});

																							return;
																						}
																						var rec = f_store.getAt(rowIndex);
																						var stencilNo = Ext.getCmp("stencilNo").getValue();//通过ruleno传递模板id
																						var areano = Ext.getCmp("area_no").getValue();//通过id传递模板区域
																						var areaType=Ext.getCmp("fodder_type").getValue();//通过fodderName传递模板区域类型
																						var rule_no=Ext.getCmp("ruleno").getValue();//通过fodderTyp规则编号
																						var areapaper = areano
																								+ "|"
																								+ Ext.getCmp("paper").getSelectionModel().getSelected().data.pid;
																						var url = "rule!viewRulePDF.action?ruleParamRes.ruleno="
																								+ stencilNo
																								+ "&ruleParamRes.fodder="
																								+ rec.data.fodder
																								+ "&ruleParamRes.pri="
																								+ rec.data.pri
																								+ "&ruleParamRes.fodderName="//传递区域类型假数据
																								+ areaType
																								+ "&ruleParamRes.fodderType="//传递区域类型假数据
																								+ rule_no
																								+ "&ruleParamRes.id="
																								+ areapaper;
																						var name = rec.data.fodder+ "_window";
																						var winparams = "height=650, width=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no";
																						window.open(url,name,winparams);
																						paperwin.hide();
																					}
																				},
																				{
																					text : "关闭",
																					handler : function() {
																						paperwin.hide();
																					}
																				} ]
																	});
														} else {
															Ext.getCmp("paper").getSelectionModel().clearSelections();
														}
														paperwin.show();
													}
												}
											});
								} else {
									var rec = f_store.getAt(rowIndex);
									var stencilNo = Ext.getCmp("stencilNo")
											.getValue();//通过ruleno传递模板id
									var areano = Ext.getCmp("area_no").getValue();//通过id传递模板区域
									var areapaper = areano;
									var areaType=Ext.getCmp("fodder_type").getValue();//通过fodderName传递模板区域类型
									var rule_no=Ext.getCmp("ruleno").getValue();//通过fodderTyp规则编号
											
									var url = "rule!viewRulePDF.action?ruleParamRes.ruleno="
											+ stencilNo
											+ "&ruleParamRes.fodder="
											+ rec.data.fodder
											+ "&ruleParamRes.pri="
											+ rec.data.pri
											+ "&ruleParamRes.fodderName="//传递区域类型假数据
											+ areaType
											+ "&ruleParamRes.fodderType="//传递区域类型假数据
											+ rule_no
											+ "&ruleParamRes.id=" + areapaper;
									var name = rec.data.fodder + "_window";
									var winparams = "height=650, width=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no";
									window.open(url, name, winparams);
								}

							}
						} ]
					} ]
		});

var clickRowIndex = 0;
var clickCellIndex = 0;

var personrule = new Ext.grid.EditorGridPanel(
		{
			id : "personrule",
			title : "个性化规则",
			width : 270,
			store : f_store,
			region : "west",
			cm : cm,
			stripeRows : true,
			headersDisabled : true,
			tbar : [
					{
						text : "增加规则",
						id : "rulePanel_add_but",
						xtype : "button",
						handler : function() {
							Ext.Ajax.request( {
										url : "rule!etRuleNoOrIdx.action",
										success : function(response) {
											
												var json = response.responseData
														|| Ext
																.decode(response.responseText);
												if (json.code == "1") {
													var Plant = personrule
															.getStore().recordType;
													var p = new Plant( {
														id : json.data,
														fodder : "",
														pri : ""
													});
													personrule.stopEditing();
													f_store.insert(0, p);
													personrule.startEditing(0,
															2);
													check_save1 = "1";
												}
											
										}
									});
						}
					},
					{
						text : "保存规则",
						id : "save_rule",
						xtype : "button",
						handler : function(e) {
							if (f_store.getCount() < 1) {
								//Ext.Msg.alert("提示","请添加数据！");
						Ext.Msg.show( {
							title : "警告",
							msg : "请添加数据!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});
						return;
					}
					if (personrule2.getSelectionModel().selection) {
						Ext.Msg.show( {
							title : "警告",
							msg : "请选择要添加运算规则的个性规则数据!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});

						return;
					}
					//判断是否有素材
				for (i = 0; i < f_store.getCount(); i++) {

					if (f_store.getAt(i).data.fodder == '') {
						Ext.Msg.show( {
							title : "警告",
							msg : "请添加第" + (i + 1) + "行，第2列的素材数据!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});

						personrule.stopEditing();
						personrule.startEditing(i, 1);
						return;
					}

					if (f_store.getAt(i).data.pri == '') {
						Ext.Msg.show( {
							title : "警告",
							msg : "请添加第" + (i + 1) + "行，第3列优先级数据!",
							buttons : Ext.Msg.OK,
							animEl : "elId",
							icon : Ext.MessageBox.WARNING
						});

						personrule.stopEditing();
						personrule.startEditing(i, 2);
						return;
					}
					for (j = i + 1; j < f_store.getCount(); j++) {
						if (f_store.getAt(i).data.pri == f_store.getAt(j).data.pri) {

							Ext.Msg.show( {
								title : "警告",
								msg : "请修改第" + (j + 1) + "行，优先级数据重复!",
								buttons : Ext.Msg.OK,
								animEl : "elId",
								icon : Ext.MessageBox.WARNING
							});

							personrule.stopEditing();
							personrule.startEditing(j, 2);
							return;
						}
					}
				}
				var modifiedArray = personrule.getStore().getModifiedRecords();
				if (modifiedArray.length == 0)
					return;
				e.setDisabled(true);
				//---------------------------------------------------------------------------------//
				var params = {};
				var ruleno = Ext.getCmp("ruleno").getValue();
				for (i = 0; i < modifiedArray.length; i++) {
					var flagRule = true;
					for (j = 0; j < params.length; j++) {
						if (params["ruleparams[" + j + "].id"] == modifiedArray[i].data.id)
							flagRule = false;
					}
					if (flagRule) {
						params["ruleparams[" + i + "].id"] = modifiedArray[i].data.id;
						params["ruleparams[" + i + "].ruleno"] = ruleno;
						params["ruleparams[" + i + "].fodder"] = modifiedArray[i].data.fodder;
						params["ruleparams[" + i + "].pri"] = modifiedArray[i].data.pri;
					}
				}
				Ext.Ajax.request( {
					url : "rule!updateListRuleParam.action",
					params : params,
					success : function(response) {
						
							var json = response.responseData
									|| Ext.decode(response.responseText);
							if (json.code == "1") {
								Ext.Msg.show( {
									title : "提示",
									msg : "保存成功!",
									buttons : Ext.Msg.OK,
									icon : Ext.MessageBox.INFO
								});
								check_save1 = "0";
								personrule.getStore().modified = [];
								f_store.reload( {
									params : {
										"ruleMaster.rule_no" : ruleno
									}
								});
							}
						

						Ext.getCmp("save_rule").setDisabled(false);
					}
				});

				//------------------------------------------------------------------------------//
			}
					},
					{
						text : "删除规则",
						xtype : "button",
						handler : function() {
							if (personrule.getSelectionModel().selection) {
								var record = personrule.getSelectionModel().selection.record.data.id;
								var ra = personrule.getStore().modified;
								var isMod = true;
								for (i = 0; i < ra.length; i++) {
									if (ra[i].data.id == record) {
										personrule.getStore().remove(ra[i]);
										personrule.getStore().modified.splice(
												i, 1);
										isMod = false;
									}
								}
								if (isMod) {
									Ext.Ajax.request( {
												url : "rule!delRuleParambyId.action",
												params : {
													"ruleParamRes.id" : record
												},
												success : function(response) {
														var json = response.responseData || Ext.decode(response.responseText);
														if (json.code == "1") {
															personrule.getStore().modified = [];
															personrule2.getStore().reload();
															f_store.reload();
															check_save1 = "0";
														}
												}
											});
								}
							} else {
								Ext.Msg.show( {
									title : "警告",
									msg : "请选择要删除的数据!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});
							}
						}
					} ],
			listeners : {
				"rowclick" : function(grid, rowIndex, e) {
					var record = grid.getStore().getAt(rowIndex);
					personrule2.getStore().reload( {
						params : {
							"ruleParamRes.id" : record.data.id
						}
					});
				},
				"celldblclick" : function(grid, rowIndex, columnIndex, e) {
					var area = Ext.getCmp("area_no").getValue();
					if (area == null || area == "")
						return;
					if (columnIndex == 1) {
						if (ISVIEW)
							return;
						clickRowIndex = rowIndex;
						clickCellIndex = columnIndex;
						fdata.baseParams["fodder.type"] = Ext.getCmp("fodder_type").getValue();
						fdata.baseParams["fodder.begin"] = Ext.getCmp("periodBegen").getValue().format("Y-m");
						fdata.baseParams["fodder.end"] = Ext.getCmp("periodEnd").getValue().format("Y-m");
						if (!win) {
							win = new Ext.Window(
									{
										layout : "fit",
										width : 1100,
										height : 500,
										closeAction : "hide",
										plain : true,
										items : [ selectFodder ],
										buttons : [
												{
													text : "确认",
													handler : function() {
														if (!Ext.getCmp("selectFodder").getSelectionModel().getSelected()) {
															Ext.Msg.show( {
																		title : "警告",
																		msg : "请选择素材!",
																		buttons : Ext.Msg.OK,
																		animEl : "elId",
																		icon : Ext.MessageBox.WARNING
																	});
															return;
														}
														var recordfodder = Ext.getCmp("selectFodder").getSelectionModel().getSelected();
														var record = personrule.getStore().getAt(clickRowIndex);
														record.set('fodder',recordfodder.data.id);
														record.set('fodderName',recordfodder.data.name);
														record.set('fodderType',recordfodder.data.type);
														var isMod = true;
														var ra = personrule.getStore().modified;
														for (i = 0; i < ra.length; i++) {
															if (ra[i].data.id == record.data.id) {
																personrule.getStore().modified[i] = record;
																isMod = false;
															}
														}
														if (isMod)
															personrule.getStore().modified[personrule.getStore().modified.length] = record;
														win.hide();
													}
												}, {
													text : "关闭",
													handler : function() {
														win.hide();
													}
												} ]
									});
						} else {
							Ext.getCmp("selectFodder").getSelectionModel().clearSelections();
						}
						Ext.getCmp('selectFodder').getBottomToolbar().doRefresh();
						win.show(this);
					}
				}
			}
		});

var ruleRow = 0;
var ruleCol = 0;

var cm2 = new Ext.grid.ColumnModel( {
	defaults : {
		sortable : false
	},
	columns : [ {
		dataIndex : "idx",
		header : "步骤编号",
		menuDisabled : true,
		width : 80,
		editor : new Ext.form.NumberField( {
			id : "idxField",
			validator : function(val) {
				if (val == "")
					return false;
				return true;
			}
		})
	}, {
		dataIndex : "type",
		header : "维度名称",
		width : 80,
		editor : fieldcombox,
		renderer : fieldRenderer,
		menuDisabled : true
	}, {
		dataIndex : "opr1",
		id:'opr1',
		header : "操作符1",
		width : 50,
		renderer : oprRenderer,
		menuDisabled : true,
		editor : {
			xtype : "billCombox",
			store : ope_store,
			id : "opr1Field",
			listeners:{
				select:function (t,val){
							personrule2.getStore().getAt(ruleRow).data.value2='';
							personrule2.getStore().getAt(ruleRow).data.valueName2='';
							personrule2.getStore().getAt(ruleRow).data.opr2='';
				}
			}			
		}
	}, {
		id:'stepValue1',
		dataIndex : "value1",
		header : "数值1",
		width : 130,
		renderer : function (input,metadata ,record){
			return record.data.valueName1;
		},
		menuDisabled : true
	}, {
		dataIndex : "opr2",
		id:'opr2',
		header : "操作符2",
		width : 50,
		renderer : oprRenderer,
		menuDisabled : true,
		editor : {
			xtype : "billCombox",
			id : "opr2Field",
			store : ope_store,
			validator:function(value){
				if(value==''){
					return true;
				}
				var val=ope_store.find('text',value);
				var record=personrule2.getStore().getAt(ruleRow);
				if(val==0){
					return "操作符不能选择等于！";
				}
				if(record.data.opr1=='1' || record.data.opr1=='3' ){
					if(val=='1' || val=='3'){
						return "请选择与之对应区间的运算符！现只能选择小于或者小于等于！请重新选择！";
					}
				}
				if(record.data.opr1=='2' || record.data.opr1=='4' ){
					if(val=='2' || val=='4'){
						return "请选择与之对应区间的运算符！现只能选择大于或者大于等于！请重新选择！";
					}
				}
				return true;
			}
		}
	}, {
		id:'stepValue2',
		dataIndex : "value2",
		header : "数值2",
		width : 130,
		renderer : function (input,metadata ,record){
			return record.data.valueName2;
		},
		menuDisabled : true
	}, {
		dataIndex : "cif",
		header : "逻辑符",
		width : 80,
		renderer : ljRenderer,
		menuDisabled : true,
		editor : ljcombox
	} ]
});


var personrule2 = new Ext.grid.EditorGridPanel(
		{
			store : rule_store,
			id : "personrule2",
			region : "center",
			title : "规则条件设置",
			columnWidth : .66,
			headersDisabled : true,
			cm : cm2,
			tbar : [
					{
						text : "增加步骤",
						xtype : "button",
						handler : function() {
							if (f_store.getCount() < 1) {
								Ext.Msg.show( {
									title : "警告",
									msg : "请先添加个性化规则数据!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});

								return;
							}
							if (!personrule.getSelectionModel().selection) {
								Ext.Msg.show( {
									title : "警告",
									msg : "请选择要添加运算规则的个性规则数据!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});

								return;
							}

							var reca = personrule.getSelectionModel().selection.record.data;
							if (reca.fodder == '' || reca.pri == '') {
								Ext.Msg.show( {
									title : "警告",
									msg : "请将个性规则数据填写完!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});

								return;
							}
							if (reca.fodder!='') {
								var type = reca.fodderType;
								if (type == 3) {
									Ext.Msg.show( {
										title : "提示",
										msg : "素材类型为列表的不能添加步骤!",
										buttons : Ext.Msg.OK,
										animEl : "elId",
										icon : Ext.MessageBox.WARNING
									});
									return;
								}
							} else {
								Ext.Msg.show( {
									title : "提示",
									msg : "请选择素材!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});
								return;
							}

							var Plant = personrule2.getStore().recordType;
							var p = new Plant( {
								idx : "",
								opr1 : "",
								value1 : "",
								opr2 : "",
								value2 : "",
								cif : "",
								type : ""
							});
							personrule2.stopEditing();
							rule_store.insert(0, p);
							personrule2.startEditing(0, 0);
							check_save2 = 1;
						}
					},
					{
						text : "保存步骤",
						id : "save_tep",
						xtype : "button",
						handler : function(e) {
							if (!personrule.getSelectionModel().selection)
								return;
							
							if (rule_store.getCount() < 1) {
								Ext.Msg.show( {
									title : "警告",
									msg : "请添加数据!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});

								return;
							}
							personrule2.stopEditing(); 
							for (i = 0; i < rule_store.getCount(); i++) {
								var data = rule_store.getAt(i).data;
								if (data.idx == '') {
									Ext.Msg.show( {
										title : "警告",
										msg : "请添加第" + (i + 1) + "行，步骤编号!",
										buttons : Ext.Msg.OK,
										animEl : "elId",
										icon : Ext.MessageBox.WARNING
									});

									personrule2.stopEditing();
									personrule2.startEditing(i, 0);
									return;
								}
								for (j = i + 1; j < rule_store.getCount(); j++) {
									if (data.idx == rule_store.getAt(j).data.idx) {
										Ext.Msg.show( {
											title : "警告",
											msg : "第" + (i + 1) + "行与第"
													+ (j + 1) + "行步骤编号重复,请修改!",
											buttons : Ext.Msg.OK,
											animEl : "elId",
											icon : Ext.MessageBox.WARNING
										});

										personrule.stopEditing();
										personrule.startEditing(j, 0);
										return;
									}
								}

								if (data.type == '') {
									Ext.Msg.show( {
										title : "警告",
										msg : "请添加第" + (i + 1) + "行，维度名称!",
										buttons : Ext.Msg.OK,
										animEl : "elId",
										icon : Ext.MessageBox.WARNING
									});

									personrule2.stopEditing();
									personrule2.startEditing(i, 1);
									return;
								}

								if (data.opr1 == "" ) {
									Ext.Msg.show( {
										title : "警告",
										msg : "请添加第" + (i + 1)
												+ "行，操作符1!",
										buttons : Ext.Msg.OK,
										animEl : "elId",
										icon : Ext.MessageBox.WARNING
									});

									personrule2.stopEditing();
									personrule2.startEditing(i, 2);
									return;
								}
								
								if (data.value1 == "" ) {
									Ext.Msg.show( {
										title : "警告",
										msg : "请添加第" + (i + 1)
												+ "行，数值1!",
										buttons : Ext.Msg.OK,
										animEl : "elId",
										icon : Ext.MessageBox.WARNING
									});

									personrule2.stopEditing();
									personrule2.startEditing(i, 3);
									return;
								}
								if(data.opr1!='0'){
									if (data.opr2 == "" ) {
										Ext.Msg.show( {
											title : "警告",
											msg : "规则条件未设置,请检查第" + (i + 1)
													+ "行，操作符2!",
											buttons : Ext.Msg.OK,
											animEl : "elId",
											icon : Ext.MessageBox.WARNING
										});
	
										personrule2.stopEditing();
										personrule2.startEditing(i, 4);
										return;
									}
									
									if (data.value2 == "" ) {
										Ext.Msg.show( {
											title : "警告",
											msg : "请添加第" + (i + 1)
													+ "行，数值2!!",
											buttons : Ext.Msg.OK,
											animEl : "elId",
											icon : Ext.MessageBox.WARNING
										});
	
										personrule2.stopEditing();
										personrule2.startEditing(i, 5);
										return;
									}
								}
								
								
								if (data.cif == '') {
										Ext.Msg.show( {
											title : "警告",
											msg : "请添加第" + (i + 1) + "行，逻辑符!",
											buttons : Ext.Msg.OK,
											animEl : "elId",
											icon : Ext.MessageBox.WARNING
										});
										personrule2.stopEditing();
										personrule2.startEditing(i, 6);
										return;
									}
								
							}
							var rpid = personrule.getSelectionModel().selection.record.data.id;
							var modifiedArray = personrule.getStore().getModifiedRecords();
							for (i = 0; i < modifiedArray.length; i++) {
								if (modifiedArray[i].data.id == rpid) {
									Ext.Msg.show( {
										title : "警告",
										msg : "请将个性规则数据先保存!",
										buttons : Ext.Msg.OK,
										animEl : "elId",
										icon : Ext.MessageBox.WARNING
									});
									return;
								}
							}
							
							if(personrule2.getStore().modified.length==0)return;

							e.setDisabled(true);
							var params = {};
							for (i = 0; i < rule_store.getCount(); i++) {
								if(rule_store.getAt(i).data.type=='birthday' ){
									if(rule_store.getAt(i).data.value1 instanceof Date){
										rule_store.getAt(i).data.value1=rule_store.getAt(i).data.value1.format('md');
									}
									
									if(rule_store.getAt(i).data.value2 instanceof Date){
										rule_store.getAt(i).data.value2=rule_store.getAt(i).data.value2.format('md');
									}
								}
								
								if( rule_store.getAt(i).data.type=='mobdate'){
									if(rule_store.getAt(i).data.value1 instanceof Date){
										rule_store.getAt(i).data.value1=rule_store.getAt(i).data.value1.format('Ymd');
									}
									
									if(rule_store.getAt(i).data.value2 instanceof Date){
										rule_store.getAt(i).data.value2=rule_store.getAt(i).data.value2.format('Ymd');
									}
								}
								params["ruleOpers[" + i + "].idx"] = rule_store.getAt(i).data.idx;
								params["ruleOpers[" + i + "].id"] = rpid;
								params["ruleOpers[" + i + "].opr1"] = rule_store.getAt(i).data.opr1;
								params["ruleOpers[" + i + "].value1"] = rule_store.getAt(i).data.value1;
								params["ruleOpers[" + i + "].opr2"] = rule_store.getAt(i).data.opr2;
								params["ruleOpers[" + i + "].value2"] = rule_store.getAt(i).data.value2;
								params["ruleOpers[" + i + "].cif"] = rule_store.getAt(i).data.cif;
								params["ruleOpers[" + i + "].type"] = rule_store.getAt(i).data.type;
								params["ruleOpers[" + i + "].valueName1"] = rule_store.getAt(i).data.valueName1;
								params["ruleOpers[" + i + "].valueName2"] = rule_store.getAt(i).data.valueName2;
							}
						
							Ext.Ajax.request( {
										url : "rule!updateListRuleOpers.action",
										params : params,
										success : function(response) {
												var json = response.responseData || Ext.decode(response.responseText);
												if (json.code == "1") {
													Ext.Msg.show( {
																title : "提示",
																msg : "保存成功!",
																buttons : Ext.Msg.OK,
																icon : Ext.MessageBox.INFO
															});
													check_save2 = 0;
													rule_store.reload();
													personrule2.getStore().modified=[];
												}
												

											Ext.getCmp("save_tep").setDisabled(false);
										}
									});
						}
					},
					{
						text : "删除步骤",
						xtype : "button",
						handler : function() {
							if (personrule2.getSelectionModel().selection) {
								var record = personrule2.getSelectionModel().selection.record.data.idx;
								var recordid=personrule2.getSelectionModel().selection.record.id;
								Ext.Ajax.request( {
											url : "rule!delRuleOperbyIdx.action",
											params : {
												"ruleOper.idx" : record
											},
											success : function(response) {
													var json = response.responseData || Ext.decode(response.responseText);
													if (json.code == "1") {
														personrule2.getStore().remove(personrule2.getStore().getById(recordid)); 
														check_save2 = 0;
														personrule2.getStore().modified=[];
													}
											}
										});

							} else {
								Ext.Msg.show( {
									title : "警告",
									msg : "请选择要删除的数据!",
									buttons : Ext.Msg.OK,
									animEl : "elId",
									icon : Ext.MessageBox.WARNING
								});

							}
						}
					} ],
					listeners:{
						'beforeedit': function (   e  ){
							var record=e.record;
							if(record.data.type!=null && record.data.type!=''){
								if( record.data.type=='sex' || record.data.type=='city' || record.data.type=='pointtype' ||
									 record.data.type=='currtype' || record.data.type=='rmbpayflag' || record.data.type=='usdpayflag'
									 || record.data.type=='jjflag' || record.data.type=='cycledue' || record.data.type=='exchtype'){
									if(ruleCol==2 || ruleCol==4 || ruleCol==5){
										validateTypeCell();
										e.cancel=true;
									}
									if(record.data.type=='city' &&  ruleCol==3 ){
										e.cancel=true;
									}
								}
								if(record.data.type=='birthday' || record.data.type=='crlim' 
										|| record.data.type=='mobilenbr' || record.data.type=='ainbr' || record.data.type=='mobdate'
										|| record.data.type=='totdueamt' || record.data.type=='cashcrlim'  || record.data.type=='currbal'){
									if(record.data.opr1=='0' || record.data.opr1==0){
										if( ruleCol==4 || ruleCol==5){
											Ext.Msg.show( {
												title : "提示",
												msg : "操作符1已是等于，所以无需输入操作符2和数值2！",
												buttons : Ext.Msg.OK,
												icon : Ext.MessageBox.WARNING
											});
											e.cancel=true;
										}
									}
								}
								
								if(!(record.data.type=='sex' || record.data.type=='city' || record.data.type=='pointtype' ||
									 record.data.type=='currtype' || record.data.type=='rmbpayflag' || record.data.type=='usdpayflag'
									 || record.data.type=='jjflag' || record.data.type=='cycledue' || record.data.type=='exchtype'|| 
									 record.data.type=='birthday' || record.data.type=='crlim'  || record.data.type=='currbal'
										|| record.data.type=='mobilenbr' || record.data.type=='ainbr' || record.data.type=='mobdate'
										|| record.data.type=='totdueamt' || record.data.type=='cashcrlim')){
									if( ruleCol==4 || ruleCol==5){
											e.cancel=true;
										}
								}
							}	
						} ,
						"celldblclick" : function(grid, rowIndex, cellIndex, e) {
							ruleRow = rowIndex;
							ruleCol = cellIndex;
							var record=personrule2.getStore().getAt(rowIndex);
							if(cellIndex==3 || cellIndex==5){
								if(record.data.type==null || record.data.type==''){
											Ext.Msg.show( {
													title : "提示",
													msg : "请先选择维度名称!",
													buttons : Ext.Msg.OK,
													animEl : "elId",
													icon : Ext.MessageBox.INFO
												});
									return;
								}
							}
							
							//加入编辑器
							if(cellIndex==3)addEdit('stepValue1',record.data.type);
							
							if(cellIndex==5)addEdit('stepValue2',record.data.type);
						}
					}
		});
var cityWin;
//添加输入框
function addEdit(field,type){
	//1==生日
	if(type=='birthday'){
		personrule2.getColumnModel().getColumnById (field).setEditor({
			xtype : "datefield",
			format : "md",
			editable : false,
			listeners:{
				select:function (  obj , date  ) {
					var value=date.format('md');
					var dirdate=date.format('m月d日');
					var record=personrule2.getStore().getAt(ruleRow);
					var checkvalue;
					var falgStep=true;
					if(ruleCol==3){
						if(record.data.value2!='' && record.data.opr2!=''){
							if(typeof(record.data.value2)=='string'){
								checkvalue=record.data.value2;
							}else{
								checkvalue=new Date(record.data.value2).format('md');
							}
							falgStep=eval(value+switchSign(record.data.opr2)+ checkvalue);
						}
					}else if(ruleCol==5){
						if(record.data.value1!='' && record.data.opr2!=''){
							if(typeof(record.data.value1)=='string'){
								checkvalue=record.data.value1;
							}else{
								checkvalue=new Date(record.data.value1).format('md');
							}
							falgStep=eval(checkvalue+switchSign(record.data.opr2)+value);
						}
					}
					//输入数值验证
					if(falgStep){
						if(ruleCol==3){
							personrule2.getStore().getAt(ruleRow).data.valueName1=dirdate;
							personrule2.getStore().getAt(ruleRow).data.value1=value;
						}
						else if(ruleCol==5){
							personrule2.getStore().getAt(ruleRow).data.valueName2=dirdate;
							personrule2.getStore().getAt(ruleRow).data.value2=value;
						}
					}else{
						alertMsg('不符合运算规则！');
					}
				}
			}
		});
	}
	//2==性别
	else if(type=='sex'){
		selectValueOnlyEquels(field,[{id:'M',text:'男'},{id:'F',text:'女'}]);
	}
	//3==城市
	else if(type=='city'){
		//选择城市树
		if(!cityWin){
			cityWin = new Ext.Window(
						{
							id:"areacity",
							title:'选择城市',
							width : 400,
							height : 500,
							closeAction : "hide",
							plain : true,
							modal :true,
							autoScroll : true,
							items : {
								id : 'updateCity',
								useArrows : true,
								animate : true,
								containerScroll : true,
								rootVisible : false,
								checkModel: 'single',   //对树的级联多选    
								onlyLeafCheckable: true,//对树所有结点都可选   
								frame : true,
								singleExpand:true,
								xtype : "treepanel",
								loader:new Ext.tree.TreeLoader({
									url:"treeCheck!city.action",
									baseAttrs:{
										uiProvider: Ext.ux.TreeCheckNodeUI
									}
								}),
								root : new Ext.tree.AsyncTreeNode(
								{
										id : "root",
										expanded : true
								})
							},
							buttons : [
									{
										text : "确认",
										handler : function() {
											var  selNodes = Ext.getCmp("updateCity").getChecked();
											if (selNodes.length == 0) {
												Ext.Msg.show( {
															title : "提示",
															msg : "请选择城市!",
															buttons : Ext.Msg.OK,
															animEl : "elId",
															icon : Ext.MessageBox.WARNING
														});
												return;
											}
											
											if(ruleCol==3){
												personrule2.getStore().getAt(ruleRow).data.value1=selNodes[0].id;
												personrule2.getStore().getAt(ruleRow).data.valueName1=selNodes[0].attributes.text;
											}
											if(ruleCol==5){
												personrule2.getStore().getAt(ruleRow).data.value2=selNodes[0].id;
												personrule2.getStore().getAt(ruleRow).data.valueName2=selNodes[0].attributes.text;
											}
											personrule2.view.getCell(ruleRow,ruleCol).firstChild.innerHTML=selNodes[0].attributes.text;
											Ext.getCmp("areacity").hide();
											//cityWin.colse();
											
										}
									}, {
										text : "关闭",
										handler : function() {
											cityWin.hide();
										}
									} ]
						}).show();
			} else {
				Ext.getCmp("updateCity").getRootNode().reload();
				cityWin.show();
			}
		
	}
	//4==积分类型:0: 无积分,1：仅携程积分,2：仅万里通积分,3：两者都有
	else if(type=='pointtype'){
		selectValueOnlyEquels(field,[{id:'0',text:'无积分'},{id:'1',text:'仅携程积分'},{id:'2',text:'仅万里通积分'},{id:'3',text:'两者都有'}]);
	}
	//5==单/双币标识:0:仅人民币,1：仅美元,2：两者都有
	else if(type=='currtype'){
		selectValueOnlyEquels(field,[{id:'0',text:'仅人民币'},{id:'1',text:'仅美元'},{id:'2',text:'两者都有'}]);
	}
	//6=="人民币自扣标示:1:有自扣，需还款,2:有自扣，无需还款,3:无自扣，需还款,4:无自扣，无需还款"
	else if(type=='rmbpayflag'){
		selectValueOnlyEquels(field,[{id:'1',text:'有自扣，需还款'},{id:'2',text:'有自扣，无需还款'},{id:'3',text:'无自扣，需还款'},{id:'4',text:'无自扣，无需还款'}]);
	}
	//7=="美元自扣标示:1:有自扣，需还款,2:有自扣，无需还款,3:无自扣，需还款,4:无自扣，无需还款"
	else if(type=='usdpayflag'){
		selectValueOnlyEquels(field,[{id:'1',text:'有自扣，需还款'},{id:'2',text:'有自扣，无需还款'},{id:'3',text:'无自扣，需还款'},{id:'4',text:'无自扣，无需还款'}]);
	}
	//8==借记交易标示:0:无交易,1：有交易
	else if(type=='jjflag'){
		selectValueOnlyEquels(field,[{id:'0',text:'无'},{id:'1',text:'有'}]);
	}
	//9==催收标识
	else if(type=='cycledue'){
		selectValueOnlyEquels(field,[{id:'0',text:'0'},{id:'1',text:'1'},{id:'2',text:'2'},{id:'3',text:'3'},{id:'4',text:'4'},{id:'5',text:'5'}
					,{id:'6',text:'6'},{id:'7',text:'7'}]);
	}
	//10==汇购标识:0:无,1：有
	else if(type=='exchtype'){
		selectValueOnlyEquels(field,[{id:'0',text:'无'},{id:'1',text:'有'}]);
	}
	//11==固定额度
	else if(type=='crlim'){
		inputNumberField(field);
	}
	//12	mobilenbr	手机号码
	else if(type=='mobilenbr'){
		inputNumberField(field);
	}
	//13	ainbr	AI码
	else if(type=='ainbr'){
		inputNumberField(field);
	}
	//14	mobdate	开户日期
	else if(type=='mobdate'){
		dateCellEdit(field);
	}
	//15	currbal	本期应付总额
	else if(type=='currbal'){
		inputNumberField(field);
	}
	//16	totdueamt	最低还款额
	else if(type=='totdueamt'){
		inputNumberField(field);
	}
	//17	cashcrlim	取现额度
	else if(type=='cashcrlim'){
		inputNumberField(field);
	}else{
		personrule2.getColumnModel().getColumnById (field).setEditor({
			xtype : "textfield"
		});
	}
}
//日期单元格编辑器
function dateCellEdit(field){
	personrule2.getColumnModel().getColumnById (field).setEditor({
			xtype : "datefield",
			format : "Ymd",
			editable : false,
			listeners:{
				select:function (  obj , date  ) {
					var value=date.format('Ymd');
					var dirdate=date.format('Y年m月d日');
					var record=personrule2.getStore().getAt(ruleRow);
					var checkvalue;
					var falgStep=true;
					if(ruleCol==3){
						if(record.data.value2!='' && record.data.opr2!=''){
							if(typeof(record.data.value2)=='string'){
								checkvalue=record.data.value2;
							}else{
								checkvalue=new Date(record.data.value2).format('Ymd');
							}
							falgStep=eval(value+switchSign(record.data.opr2)+ checkvalue);
						}
					}else if(ruleCol==5){
						if(record.data.value1!='' && record.data.opr2!=''){
							if(typeof(record.data.value1)=='string'){
								checkvalue=record.data.value1;
							}else{
								checkvalue=new Date(record.data.value1).format('Ymd');
							}
							falgStep=eval(checkvalue+switchSign(record.data.opr2)+value);
						}
					}
					//输入数值验证
					if(falgStep){
						if(ruleCol==3){
							personrule2.getStore().getAt(ruleRow).data.valueName1=dirdate;
							personrule2.getStore().getAt(ruleRow).data.value1=value;
						}
						else if(ruleCol==5){
							personrule2.getStore().getAt(ruleRow).data.valueName2=dirdate;
							personrule2.getStore().getAt(ruleRow).data.value2=value;
						}
					}else{
						alertMsg('不符合运算规则！');
					}
				}
			}
		});
}
//下拉选择框，只能等于的
function selectValueOnlyEquels(field,data){
	personrule2.getColumnModel().getColumnById (field).setEditor({
			xtype : "billCombox",
			store:new Ext.data.JsonStore( {
				fields : [ "id", "text" ],
				data : data
			}),
			listeners:{
				'select' : function (  combo ,  record ,  index  ) {
					//选择后清空value1和value2的值
					if(ruleCol==3)personrule2.getStore().getAt(ruleRow).data.valueName1=record.data.text;
				}
			}			
		});
}
//数字输入框
function inputNumberField(field){
	personrule2.getColumnModel().getColumnById (field).setEditor({
			xtype : "numberfield",
			validationDelay:500,
			validator:function (value){
				if(value==""){
					return true;
				}
				var record=personrule2.getStore().getAt(ruleRow);
				var checkValue;
				var flagNum=true;
				//输入数值验证
				if(ruleCol==3){
					if(!(record.data.value2=='' || record.data.opr2=='')){
						flagNum=eval(value+switchSign(record.data.opr2)+record.data.value2);
					}
				}
				
				if(ruleCol==5){
					if(!(record.data.value1=='' || record.data.opr2=='')){
						flagNum=eval(record.data.value1+switchSign(record.data.opr2)+value);
					}
				}
				if(flagNum){
					if(ruleCol==3)personrule2.getStore().getAt(ruleRow).data.valueName1=value;
					if(ruleCol==5)personrule2.getStore().getAt(ruleRow).data.valueName2=value;
					return true;
				}else{
					alertMsg('不符合运算规则！');
					return false;
				}
			}
		});
}
//控制编辑
function validateTypeCell(field){
	if(ruleCol==2){
		Ext.Msg.show( {
								title : "提示",
								msg : "您选择的维度只能选择等于操作！",
								buttons : Ext.Msg.OK,
								icon : Ext.MessageBox.WARNING
							});
	}
	if(ruleCol==4 || ruleCol==5){
		Ext.Msg.show( {
			title : "提示",
			msg : "您选择的维度无需输入操作符2和数值2！",
			buttons : Ext.Msg.OK,
			icon : Ext.MessageBox.WARNING
		});
	}	
}
//转换符号
function switchSign(val){
		if(val=='0')return '==';
		if(val=='1')return '>';
		if(val=='2')return '<';
		if(val=='3')return '>=';
		if(val=='4')return '<=';
}
//详细显示图片区域
var images_panel = {
	id : "view3",
	xtype : "panel",
	title : "个性化区域图片",
	id : "imagePanel",
	store : image_store,
	width : 200,
	height : 302,
	buttonAlign : "center",
	bbar : new Ext.Toolbar( {
		buttonAlign : "center",
		items : [
				{
					icon : "images/shared/icons/fam/collapse.gif", // Use a URL in the icon config
					tooltip : "上一张",
					handler : function() {
						if (image_store.getCount() == 0) {
							Ext.Msg.show( {
								title : "提示",
								msg : "请选择模板!",
								buttons : Ext.Msg.OK,
								icon : Ext.MessageBox.WARNING
							});
							return;
						}
						if (img_index == 0) {
							img_index = image_store.getCount();
						}
						img_index -= 1;
						var page_num = (img_index + 1) + "/"+ image_store.getCount();
						Ext.getCmp("show_num").setText(page_num);
						var rec = image_store.getAt(img_index);
						var win = Ext.getCmp("imagePanel");
						win.show();
						var html = "<a href=javascript:show_img('"
								+ encodeURI(encodeURI(rec.get('path'))) + "');>"
								+ "<img width=" + win.getInnerWidth()
								+ " height=" + win.getInnerHeight()
								+ " src='download.action?id="
								+ encodeURI(rec.get('path')) + "'/></a>";
						win.update(html);
					}
				},
				{
					id : "show_num",
					xtype : "tbtext"
				},
				{
					icon : "images/shared/icons/fam/expand.gif", // Use a URL in the icon config
					tooltip : "下一张",
					handler : function() {
						if (image_store.getCount() == 0) {
							Ext.Msg.show( {
								title : "提示",
								msg : "请选择模板!",
								buttons : Ext.Msg.OK,
								icon : Ext.MessageBox.WARNING
							});
							return;
						}
						if (img_index == image_store.getCount() - 1) {
							img_index = -1;
						}
						img_index += 1;
						var page_num = (img_index + 1) + "/"
								+ image_store.getCount();
						Ext.getCmp("show_num").setText(page_num);
						var rec = image_store.getAt(img_index);
						var win = Ext.getCmp("imagePanel");
						win.show();
						var html = "<a href=javascript:show_img('"
								+ encodeURI(rec.get('path')) + "');>"
								+ "<img width=" + win.getInnerWidth()
								+ " height=" + win.getInnerHeight()
								+ " src='download.action?id="
								+ encodeURI(rec.get('path')) + "'/></a>";
						win.update(html);
					}
				} ]
	})
};

var p3 = new Ext.Panel(
		{
			autoScroll : true,
			layout : "border",
			border : false,
			items : [ {
				region : "north",
				height : 301,
				border : false,
				layout : 'column',
				items : [ images_panel, p2 ]
			}, personrule, personrule2 ],
			buttonAlign : "center",
			buttons : [
					{
						text : "提交审核",
						id : "submitStart",
						xtype : "button",
						handler : function() {
						var areaform = Ext.getCmp("areaform");
							Ext.Ajax.request( {
								clientValidation : true,
								params : {
									"ruleMaster.rule_no" : areaform.getComponent("rule_no").getValue(),
									"ruleMaster.rule_state":3
								},
								url : "rule!updState.action",
								success : function(response, opts) {
									var d = eval('(' + response.responseText + ')');
									if (d.code == '1') {
										Ext.getCmp('p1Grid').getBottomToolbar().doRefresh();
										Ext.Msg.show( {
													title : "提示",
													msg : "已提交审核!",
													buttons : Ext.Msg.OK,
													icon : Ext.MessageBox.INFO
										});

										Ext.getCmp("rule_panel").layout.setActiveItem(0);
									} else {
										Ext.Msg.show( {
													title : "提示",
													msg : "操作失败!",
													buttons : Ext.Msg.OK,
													icon : Ext.MessageBox.ERROR
										});
									}
								}
							});
						}
					},
					{
						text : "保存",
						id : "saveButton",
						xtype : "button",
						handler : function(e) {
							//表单验证
							if (!Ext.getCmp("areaform").getForm().isValid()) {
								Ext.Msg.show( {
									title : "提示",
									msg : "验证不通过!",
									buttons : Ext.Msg.OK,
									icon : Ext.MessageBox.ERROR
								});

								return;
							}
							//从表与从从表修改数据是否保存验证
							if (check_save1 == "1"
									&& !Ext.getCmp("rulePanel_add_but").disabled) { //从表数据未保存
								Ext.Msg.show( {
									title : "提示",
									msg : "个性化规则尚未保存，请先保存!",
									buttons : Ext.Msg.OK,
									icon : Ext.MessageBox.WARNING
								});
								return;
							}
							if (check_save2 == "1"
									&& !Ext.getCmp("rulePanel_add_but").disabled) { //从从表数据未保存
								Ext.Msg.show( {
									title : "提示",
									msg : "规则条件尚未保存，请先保存!",
									buttons : Ext.Msg.OK,
									icon : Ext.MessageBox.WARNING
								});
								return;
							}
							
							var carid="";
							if (prod_opera != "rule!upd.action") {
								carid=Ext.getCmp("areaform").getComponent("cardno").getValue();
							}

							e.setDisabled(true);
							//个性化区域主表类别，模板，区域，优先级唯一
							Ext.Ajax.request( {
										clientValidation : true,
										params : {
											"ruleMaster.card_id" : carid,
											"ruleMaster.rule_type" : Ext
													.getCmp("areaform")
													.getComponent("rule_type")
													.getValue(),
											"ruleMaster.stencil_no" : Ext
													.getCmp("areaform")
													.getComponent("stencil_no")
													.getValue(),
											"ruleMaster.areaNo" : Ext.getCmp(
													"areaform").getComponent(
													"areaNo").getValue(),
											"ruleMaster.rule_pri" : Ext.getCmp(
													"areaform").getComponent(
													"rule_pri").getValue()
										},
										url : "rule!queryKeyIsValid.action",
										success : function(response, opts) {
											var d = eval("("
													+ response.responseText
													+ ")");
											if (d.code == "0") { //数据库未存在该记录，允许添加
												var areaform = Ext.getCmp("areaform");
												var stenciltypedate =temp_sotre.getById(areaform.getComponent("stencil_no").getValue());
												if(!stenciltypedate){
													alertMsg("模板已不存在!");
													return;
												}
												var stenciltype = stenciltypedate.data.type;
												var typeName;
												var rule_name; 
												if (prod_opera == "rule!upd.action") {
													rule_name = areaform.getComponent("rule_name").getValue();
												}else{
													if(stenciltype == "1"){typeName = "纸质";}
													else if(stenciltype == "2"){typeName = "电子";}
													rule_name = areaform.getComponent("rule_name").getValue()+"["+typeName+"]";
												}
												
												//添加模板类型标识
												var rulename = Ext.getCmp(
														"areaform")
														.getComponent(
																"stencil_no").lastSelectionText;//获得下拉框的显示文本
												Ext.Ajax.request( {
															clientValidation : true,
															params : {
																"ruleMaster.card_id" : areaform
																		.getComponent(
																				"cardno")
																		.getValue(),
																"ruleMaster.rule_no" : areaform
																		.getComponent(
																				"rule_no")
																		.getValue(),
																"ruleMaster.rule_name" : rule_name,
																"ruleMaster.rule_type" : areaform
																		.getComponent(
																				"rule_type")
																		.getValue(),
																"ruleMaster.stencil_no" : areaform
																		.getComponent(
																				"stencil_no")
																		.getValue(),
																"ruleMaster.areaNo" : areaform
																		.getComponent(
																				"areaNo")
																		.getValue(),
																"ruleMaster.period_begen" : areaform
																		.getComponent(
																				"period_begen")
																		.getValue(),
																"ruleMaster.period_end" : areaform
																		.getComponent(
																				"period_end")
																		.getValue(),
																"ruleMaster.rule_pri" : areaform
																		.getComponent(
																				"rule_pri")
																		.getValue(),
																"ruleMaster.rule_state" : areaform
																		.getComponent(
																				"rule_state")
																		.getValue(),
																"ruleMaster.remark" : areaform
																		.getComponent(
																				"remark")
																		.getValue(),
																"ruleMaster.fodderType" : areaform
																		.getComponent(
																				"fodderType")
																		.getValue()
															},
															url : prod_opera,
															success : function(
																	response,
																	opts) {

																var d = eval('(' + response.responseText + ')');
																if (d.code == '1') {
																	Ext.getCmp('p1Grid').getBottomToolbar().doRefresh();
																	Ext.Msg.show( {
																				title : "提示",
																				msg : "操作成功!",
																				buttons : Ext.Msg.OK,
																				icon : Ext.MessageBox.INFO
																			});
																	e.setDisabled(false);
																	Ext.getCmp("rulePanel_add_but").setDisabled(false); //允许增加规则
																	prod_opera = "rule!upd.action";
																	stenciltype = temp_sotre.getById(Ext.getCmp('stencilNo')
																					.getValue()).data.type;

																	Ext.getCmp('ruleType').setDisabled(true);
																	Ext.getCmp('stencilNo').setDisabled(true);
																	Ext.getCmp('area_no').setDisabled(true);
																	Ext.getCmp('rule_name').setDisabled(true);
																	Ext.getCmp("submitStart").setVisible(true);
																	
																	
																	//Ext.getCmp("rule_panel").layout.setActiveItem(0);
																} else {
																	Ext.Msg.show( {
																				title : "提示",
																				msg : "操作失败!",
																				buttons : Ext.Msg.OK,
																				icon : Ext.MessageBox.ERROR
																			});

																}
																Ext.getCmp("saveButton").setDisabled(false);
															}
														});

											} else if (d.code == '9') {
												Ext.Msg.show( {
															title : "警告",
															msg : "规则唯一性验证错误!",
															buttons : Ext.Msg.OK,
															animEl : "elId",
															icon : Ext.MessageBox.WARNING
														});

											} else {
												Ext.Msg.show( {
															title : "警告",
															msg : "数据库已存在该记录!",
															buttons : Ext.Msg.OK,
															animEl : "elId",
															icon : Ext.MessageBox.WARNING
														});

											}
											Ext.getCmp("saveButton").setDisabled(false);
										}
									});
						}
					},
					{
						text : "取消",
						id : "cancelButton",
						xtype : "button",
						handler : function() {
							Ext.getCmp("rule_panel").layout.setActiveItem(0);
							Ext.getCmp("areaform").setDisabled(false);//disabled=true;
							Ext.getCmp("fodder_type").setDisabled(true);
							Ext.getCmp("ruleState").setDisabled(true);
							Ext.getCmp("personrule").getTopToolbar()
									.setDisabled(false);
							Ext.getCmp("personrule2").getTopToolbar()
									.setDisabled(false);
							Ext.getCmp("submitStart").setVisible(true);
							Ext.getCmp("saveButton").setVisible(true);
							ISVIEW = false;
							setRulePanelDisable(false);
							if (Ext.getCmp("cancelButton").getText() == '返回') {
								Ext.getCmp("cancelButton").setText("取消");
							}
						}
					} ]

		});

var rule_pel = new Ext.Panel( {
	id : "master_panel",
	border : false,
	layout : "border",
	items : [ {
		id : "prod_tree",
		xtype : "billTreePanel",
		dataUrl : "tree!product.action",
		root : new Ext.tree.AsyncTreeNode( {
			id : "root",
			expanded : true
		// 自动展开节点
				}),
		listeners : {
			"click" : function(node) {
				if (node.isLeaf()) {
					Ext.getCmp("a").setValue("");//清空查询项
		Ext.getCmp("b").setValue("");//清空查询项
		Ext.getCmp("c").setValue("");//清空查询项
		store.baseParams = store.baseParams || {};
		store.baseParams["ruleMaster.card_id"] = node.id;
		store.baseParams["ruleMaster.rule_type"] = "";
		store.baseParams["ruleMaster.stencil_no"] = "";
		store.baseParams["ruleMaster.stenciltype_no"] = "";
		store.load( {
			params : {
				start : 0,
				limit : 18
			}
		});
		Ext.getCmp("rule_panel").layout.setActiveItem(0);
		temp_sotre.load( {
			params : {
				"template.cardid" : node.id,
				"template.state" : 1
			}
		});
		Ext.getCmp("areaform").setDisabled(false);//disabled=true;
		Ext.getCmp("fodder_type").setDisabled(true);
		Ext.getCmp("ruleState").setDisabled(true);
		Ext.getCmp("personrule").getTopToolbar().setDisabled(false);
		Ext.getCmp("personrule2").getTopToolbar().setDisabled(false);
		Ext.getCmp("submitStart").setVisible(true);
		Ext.getCmp("cancelButton").setVisible(true);
		Ext.getCmp("saveButton").setVisible(true);
		setRulePanelDisable(false);
		ISVIEW = false;
	}
}
}
	}, {
		region : "center",
		border : false,
		id : "rule_panel",
		layout : "card",
		activeItem : 0,//显示第一个容器
		items : [ p1, p3 ]
	} ]
});
field_store.load();
var CenterPanel = Ext.getCmp("CenterPanel");
CenterPanel.removeAll();
CenterPanel.add(rule_pel);
CenterPanel.doLayout();
</script>